package com.gxufl.console;

import java.util.List;
import java.util.Scanner;

import com.gxufl.entity.Notice;
import com.gxufl.entity.Room;
import com.gxufl.entity.Ticket;
import com.gxufl.service.NoticeService;
import com.gxufl.service.RoomService;
import com.gxufl.service.TicketService;
import com.gxufl.util.BackException;
import com.gxufl.util.InputUtil;

public class TicketConsole {

	NoticeService noticeService = new NoticeService();
	RoomService roomService = new RoomService();
	TicketService ticketService = new TicketService();

	/**
	 * 处理保存票据逻辑
	 * @throws BackException 
	 */
	public void saveTicket() throws BackException {
		String noticename = InputUtil.nextInput("请输入电影名称：");
		String roomname = InputUtil.nextInput("请输入影厅名称：");
		String seatcode = InputUtil.nextInput("请输入座位编号：");
		Integer price = InputUtil.nextIntInput("请输入购买价格：");
		String usertel = InputUtil.nextInput("请输入观众手机号：");

		// 获取电影实体
		Notice notice = noticeService.searchOneNotice(noticename);
		Integer noticeid = notice.id;

		// 获取影厅实体
		Room room = roomService.searchOneRoom(roomname);
		Integer roomid = room.id;

		Ticket ticket = new Ticket(noticeid, noticename, usertel, roomid,
				roomname, seatcode, price);
		ticketService.saveTicket(ticket);
	}

	public void searchTicket() throws BackException {
		String noticeNameAndRoomName = InputUtil
				.nextInput("请输入电影名称以及影厅（用英文逗号相隔，如：“猫妖传,朱瑾花厅”）：");
		String[] split = noticeNameAndRoomName.split(",");
		String noticeName = split[0];
		String roomName = split[1];

		List<Ticket> list = ticketService.searchTicket(noticeName, roomName);

		System.out.println("电影《" + noticeName + "》在" + roomName + "已售出"
				+ list.size() + "张票");
	}
	
	
	public void searchTicketByCode() throws BackException {
		String usertel = InputUtil.nextInput("请输入观众手机号：");
		Ticket ticket = ticketService.findOneByUsertel(usertel);
		if (ticket!= null) {
			// TODO: 完善票信息显示
			System.out.println("ticket");
		} else {
			System.out.println("找不到该观众的购票信息！");
		}
	}
}
